package com.ygg.bootservice.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ygg.bootcommon.config.NotIntercept;
import com.ygg.bootcommon.model.bo.ReturnBO;
import com.ygg.bootcommon.model.bo.TokenBO;
import com.ygg.bootcommon.model.entity.SerialCode;
import com.ygg.bootservice.service.ISerialCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2020/12/18 14:48
 * @description：序列号控制层
 */
@RestController
@RequestMapping("api/serial")
public class SerialCodeController {
    @Autowired
    private ISerialCodeService iSerialCodeService;

    @RequestMapping(value = "/generateSerialCode", method = RequestMethod.POST)
    public String generateSerialCode(@RequestBody Map<String,String> map) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            String serialCode = iSerialCodeService.generateSerialCode(map);
            ReturnBO bo = new ReturnBO();
            bo.setState(0);
            bo.setObject(serialCode);
            return gson.toJson(bo);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("生成序列码失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }

    @RequestMapping(value = "/getAllSerialCode", method = RequestMethod.POST)
    @NotIntercept
    public String getAllSerialCode(@RequestBody Map<String,String> map) throws Exception{
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            List<SerialCode> list= iSerialCodeService.getAllSerialCode(map);
            ReturnBO bo = new ReturnBO();
            bo.setState(0);
            bo.setObject(list);
            return gson.toJson(bo);
        }catch(Exception e){
            e.printStackTrace();
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("查询失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }

}
